const Directory  = require('../index')
const themeConfig ={
    nav:[
        {text:'主页',link:'/'},
    ],
    sidebar:{},
};
const fs = require('fs');
const rpath = require('path');
// 获取根目录
var DOCS_PATH = rpath.resolve(__dirname, '../');
Directory.forEach(item=>{
    // 生成nav 
    const navItem = {
        text:item.text,
        link:`/${item.path}/`
    }
    if(item.nav){
        // 查看是否有对应的title
       const navItemsIndex =  themeConfig.nav.findIndex(i=>i.text === item.nav);
       if(navItemsIndex>=0){
        // 说明存在 直接添加
        // 该处判断是否有subNav
        if(item.subNav){
            const subNavIndex = themeConfig.nav[navItemsIndex].items.findIndex(i=>i.text === item.subNav)
            if(subNavIndex>=0){
                themeConfig.nav[navItemsIndex].items[subNavIndex].items.push(navItem) 
            } else {
                themeConfig.nav[navItemsIndex].items.push({
                    text:item.subNav,
                    items:[navItem]
                })
            }
        }else{
            themeConfig.nav[navItemsIndex].items.push(navItem);
        }
       }else{
        if(item.subNav){
            themeConfig.nav.push({
                text:item.nav,
                items:[{
                    text: item.subNav,
                    items: [navItem]
                }]
            })
        }else{
            themeConfig.nav.push({
                text:item.nav,
                items:[navItem]
            })
        }
       }
    }else{
        themeConfig.nav.push(navItem)
    }
    themeConfig.nav.push()
    let file_list = fs.readdirSync(rpath.resolve(DOCS_PATH, item.path));
    const sidebarList = file_list.map(item2=>{
        if(!item2.endsWith('.md')){
            const COM_MESSAGE = require(rpath.resolve(DOCS_PATH, item.path+'/'+item2));
            const childrenList = [];
            (fs.readdirSync(rpath.resolve(DOCS_PATH, item.path+'/'+item2)))
            .forEach(item3=>{
                if(item3.endsWith('.md')){
                    childrenList.push(`/${item.path}/${item2}/${item3.replace('.md','')}`)
                }
            });
            return {
                ...COM_MESSAGE,
                children:childrenList,
            }
        }
        if(item2.endsWith('.md') && item2.indexOf('README') == -1){
            return item2.replace('.md','')
        }
        return ''
    }).filter(item=>item!=='')
    // 将README顺序提升到第一菜单
    themeConfig.sidebar[`/${item.path}/`] = ['',...sidebarList];
})
module.exports = {
  title: '萤火虫前端组件库',
  plugins: ['demo-container',['one-click-copy',{
    copySelector: ['div[class*="language-"] pre', 'div[class*="aside-code"] aside'],
    copyMessage: '复制成功',
    toolTipMessage: '复制到剪切板',
    duration: 1000,
  }],'@vuepress/back-to-top'],
  markdown: {},
  markdown: {
    lineNumbers: false,
  },
  themeConfig: {
    ...themeConfig,
    logo:'/images/logo.png',
    /*自定义的配置*/
    simulatorUrl: {
    }
  },
  configureWebpack: {
    node: {
      global: true,
      process: true
    },
  }
}
